// pages/user/requirement/requirement.js
import { CreateOrder } from '../../../api/order.js'
const moment = require('moment')

let _currentDateStr = moment().format('YYYY-MM-DD');
let _endDateStr = moment().add(1, 'days').format('YYYY-MM-DD');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentDate: _currentDateStr,
    roomTypes: ['基本房型', '大床房', '双床房'],
    region: ['上海市', '上海市', '宝山区'],
    building: '',
    hotelName: '',
    startDate: _currentDateStr,
    endDate: _endDateStr,
    endDateStart: _endDateStr,
    roomType: '0',
    dayCount: 1,
    breakfast: false,
    noSmoking: false,
    administratived: false,
    snp: false,
    hotelInvoice: false,
    housemate: true,
    persons: [],
    children: [],
    budget: 500,
    arriveTime: '14:00'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindRegion(e) {
    this.setData({
      region: e.detail.value
    })
  },
  bindBuilding(e) {
    this.setData({
      building: e.detail.value
    })
  },
  bindHotelName(e) {
    this.setData({
      hotelName: e.detail.value
    })
  },
  bindStartDate(e) {
    const currentDate = moment(_currentDateStr, 'YYYY-MM-DD');
    const startDate = moment(e.detail.value, 'YYYY-MM-DD');
    const endDate = startDate.add(1, 'days');

    if (startDate.isBefore(currentDate)) {
      wx.showToast({ title: '入住日期不能早于当前日期', icon: 'none', duration: 2000 });
      return;
    }

    this.setData({
      startDate: e.detail.value,
      endDate: endDate.format('YYYY-MM-DD'),
      endDateStart: endDate.format('YYYY-MM-DD'),
      dayCount: 1,
    });
  },
  bindEndDate(e) {
    const startDate = moment(this.data.startDate, 'YYYY-MM-DD');
    const endDate = moment(e.detail.value, 'YYYY-MM-DD');
    if (endDate.isBefore(startDate)) {
      wx.showToast({ title: '退房日期不能早于入住日期', icon: 'none', duration: 2000 });
      return;
    }

    this.setData({
      endDate: e.detail.value,
      dayCount: endDate.diff(startDate, 'days')
    })
  },
  bindBreakfast(e) {
    this.setData({
      breakfast: e.detail.value
    })
  },
  bindNoSmoking(e) {
    this.setData({
      noSmokeing: e.detail.value
    });
  },
  bindAdministratived(e) {
    this.setData({
      bindAdministratived: e.detail.value
    })
  },
  bindSnp(e) {
    this.setData({
      snp: e.detail.value
    })
  },
  bindHotelInvoice(e) {
    this.setData({
      hotelInvoice: e.detail.value
    })
  },
  bindHousemate(e) {
    this.setData({
      housemate: e.detail.value
    })
  },
  addPerson(e) {
    this.data.persons.push({
      name: ''
    });
    this.setData({
      persons: this.data.persons
    });
  },
  delPerson(e) {
    this.data.persons.pop();
    this.setData({
      persons: this.data.persons
    })
  },
  bindPersonItem(e) {
    let index = e.currentTarget.dataset.index;
    this.data.persons[index].name = e.detail.value;
    this.setData({
      persons: this.data.persons
    });
  },
  addChild(e) {
    this.data.children.push({
      name: ''
    });
    this.setData({
      children: this.data.children
    });
  },
  delChild(e) {
    this.data.children.pop();
    this.setData({
      children: this.data.children
    });
  },
  bindChildrenItem(e) {
    let index = e.currentTarget.dataset.index;
    this.data.children[index].name = e.detail.value;
    this.setData({
      children: this.data.children
    });
  },
  bindBudget(e) {
    this.setData({
      budget: e.detail.value
    });
  },
  bingRoomType(e) {
    this.setData({
      roomType: e.detail.value
    });
  },
  bindArriveTime(e) {
    this.setData({
      arriveTime: e.detail.value
    });
  },
  createOrder(e) {
    if (this.data.hotelName === '') {
      wx.showToast({ title: '请输入酒店名称', icon: 'none', duration: 2000 });
      return;
    } else if (moment(this.data.startDate, 'YYYY-MM-DD').isBefore(moment(_currentDateStr))) {
      wx.showToast({ title: '入住日期不能早于当前日期', icon: 'none', duration: 2000 });
      return;
    } else if (moment(this.data.endDate, 'YYYY-MM-DD').isBefore(moment(this.data.startDate, 'YYYY-MM-DD'))) {
      wx.showToast({ title: '退房日期不能早于入住日期', icon: 'none', duration: 2000 });
      return;
    } else if (this.data.budget <= 0) {
      wx.showToast({ title: '请输入预算金额', icon: 'none', duration: 2000 });
    }

    const params = {
      orderType: 'SINGLE_ROOM',
      region: this.data.region,
      hotelName: this.data.hotelName,
      plannedCheckInDate: this.data.startDate,
      plannedCheckOutDate: this.data.endDate,
      dayCount: this.data.dayCount,
      singleRoomBudget: this.data.budget,
      roomInfos: [
        {
          roomCount: 1,
          roomType: this.data.roomType == 0 && 'BaseType' ||
            this.data.roomType == 1 && 'BigBed' ||
            this.data.roomType == 2 && 'DoubleBed' ||
            null
        }
      ],
      totalRoomCount: 1,
      totalPeopleCount: 1,
      kidCount: 0,
      additionalBudget: 0,
      totalBudget: this.data.budget,
      needBreakfast: this.data.breakfast,
      noSmoking: this.data.noSmokeing,
      needHotelInvoice: this.data.hotelInvoice,
      arriveTime: this.data.arriveTime,
      allowedHousemate: this.data.housemate
    };
    CreateOrder(params)
      .then(data => {
        wx.showToast({ title: '订单创建成功', icon: 'none', duration: 2000 });
        wx.navigateBack({});
      })
      .catch(err => {
        console.log(err);
        wx.showToast({ title: err.message, icon: 'none', duration: 2000 });
      })
  },
  toHome() {
    wx.navigateBack({
      delta: 2
    });
  }

});